package com.spartez.farhorizons.client;

import com.google.gwt.user.client.ui.HTML;
import com.gwtext.client.core.Connection;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.Window;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.Field;
import com.gwtext.client.widgets.form.Form;
import com.gwtext.client.widgets.form.FormPanel;
import com.gwtext.client.widgets.form.Label;
import com.gwtext.client.widgets.form.NumberField;
import com.gwtext.client.widgets.form.event.FormListener;
import com.gwtext.client.widgets.form.event.FormListenerAdapter;

public class TechCalculator extends Window {

	private final NumberField currentTech = new NumberField("Current tech level");
	private final NumberField desiredTech = new NumberField("Future tech level");
	
	private final NumberField baseCost = new NumberField("Base cost");
	private final NumberField aidedCost = new NumberField("Aided cost");
	private final NumberField guaranteedCost = new NumberField("Guaranteed cost");

	private final Button calcButton = new Button("Calculate");
	private final Button closeButton = new Button("Close");
	
	private final ButtonListenerAdapter buttonListener = new ButtonListenerAdapter() {
		public void onClick(Button button, com.gwtext.client.core.EventObject e) {
			if (button == calcButton) {
				calculate();
			} else {
				close();
			}
		};
	};

	public TechCalculator() {
		super("Research Cost Calculator", true, false);
		//setIconCls("paste-icon");  
		setResizable(false);  
		
		final FormPanel formPanel = new FormPanel();
		formPanel.setButtons(new Button[] {calcButton, closeButton});
		calcButton.setType("submit");
		
		baseCost.setReadOnly(true);
		aidedCost.setReadOnly(true);
		guaranteedCost.setReadOnly(true);
		
		formPanel.add(currentTech);
		formPanel.add(desiredTech);
		formPanel.add(new HTML("<p>"));
		formPanel.add(baseCost);
		formPanel.add(aidedCost);
		formPanel.add(guaranteedCost);
		add(formPanel);
		
		calcButton.addListener(buttonListener);
		closeButton.addListener(buttonListener);
		
		currentTech.setValue(FarHorizons.dataService.getCurrentGravity());
		desiredTech.setValue(FarHorizons.dataService.getCurrentGravity() + 1);
		
		currentTech.setAllowBlank(false);
		desiredTech.setAllowBlank(false);

		currentTech.setMinValue(1);
		desiredTech.setMinValue(1);

		currentTech.setMaxValue(200);
		desiredTech.setMaxValue(200);

		currentTech.setAllowDecimals(false);
		desiredTech.setAllowDecimals(false);
		baseCost.setAllowDecimals(false);
		aidedCost.setAllowDecimals(false);
		guaranteedCost.setAllowDecimals(false);
		
		currentTech.setAllowNegative(false);
		desiredTech.setAllowNegative(false);
		baseCost.setAllowNegative(false);
		aidedCost.setAllowNegative(false);
		guaranteedCost.setAllowNegative(false);
		
		baseCost.setTabIndex(-1);
		aidedCost.setTabIndex(-1);
		guaranteedCost.setTabIndex(-1);
		calcButton.setTabIndex(-1);
		closeButton.setTabIndex(-1);
	}

	protected void calculate() {
		final int current = currentTech.getValue().intValue();
		final int desired = desiredTech.getValue().intValue();
		if (desired > current) {
			int base = 0;
			for (int i = current; i < desired; i++) {
				base += i * i;
			}
			
			int sure = base;
			for (int i = desired; i < 2 * desired - current; i ++) {
				sure += i * i;
			}
			
			baseCost.setValue(base);
			aidedCost.setValue(base - base/4);
			guaranteedCost.setValue(sure);
		}
	}
}
